﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkManagement;

namespace LorraineSec.MessageLib.Messages
{
    public sealed class AuthResponseMessage : EncryptedMessage
    {
        #region Format Constants
        /*
         * Format:
         *     4           1             1
         * | UserID | LoginStatus | SessionID |
         * 
         */

        private const int ENC_USERID_LENGTH = 4;
        private const int ENC_STATUS_LENGTH = 1;
        private const int ENC_SESSID_LENGTH = 1;

        private const int ENC_USERID_OFFSET = 0;
        private const int ENC_STATUS_OFFSET = ENC_USERID_OFFSET + ENC_USERID_LENGTH;
        private const int ENC_SESSID_OFFSET = ENC_STATUS_OFFSET + ENC_STATUS_LENGTH;

        private const int ENC_KNOWN_LENGTH = ENC_USERID_LENGTH + ENC_STATUS_LENGTH + ENC_SESSID_LENGTH;
        #endregion

        public enum Status
        {
            APPROVED,
            DENIED,
            UNACCEPTED
        }

        public uint UserID;
        public Status LoginStatus;
        public byte SessionID;

        public AuthResponseMessage()
        {
            init();
        }

        public AuthResponseMessage(byte[] bytes)
            : base(bytes)
        {
            init();
            decode();
        }

        private void init()
        {
            MessageID = (ushort)LorraineSecMessageFactory.MessageID.AUTH_RESPONSE;
        }

        public override void decrypt(CryptoManager crypt)
        {
            //Decrypt byte array
            base.decrypt(crypt);
            //Decode byte array
            UserID = Bytes.getUInt32(encData, ENC_USERID_OFFSET);
            LoginStatus = (Status)Bytes.getByte(encData, ENC_STATUS_OFFSET);
            SessionID = Bytes.getByte(encData, ENC_SESSID_OFFSET);
        }

        public override void encrypt(CryptoManager crypt)
        {
            //Encode Byte Array
            encData = new byte[ENC_KNOWN_LENGTH];
            Bytes.setValue(encData, ENC_USERID_OFFSET, UserID);
            Bytes.setValue(encData, ENC_STATUS_OFFSET, (byte)LoginStatus);
            Bytes.setValue(encData, ENC_SESSID_OFFSET, SessionID);
            //Encrypt Byte Array
            base.encrypt(crypt);
        }
    }
}
